/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db2docbook;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author alhshk
 */
public class Table implements Comparable<Table> {

  final String name;
  final String catalog;
  final String schema;
  final Map<String, Column> columns = new LinkedHashMap<String, Column>();
  final Set<String> primaryKeys = new LinkedHashSet<String>();
  private Database database;

  public Table(String catalog, String schema, String name) {
    if (name == null) {
      throw new NullPointerException("Name cannot be null");
    }
    this.name = name;
    this.catalog = catalog;
    this.schema = schema;
  }

  public String getCatalog() {
    return catalog;
  }

  public String getName() {
    return name;
  }

  public String getSchema() {
    return schema;
  }

  void addPrimaryKey(String colName) {
    primaryKeys.add(colName);
  }

  void setDatabase(Database database) {
    this.database = database;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final Table other = (Table) obj;
    if (!this.name.equals(other.name)) {
      return false;
    }
    if (this.catalog != other.catalog && (this.catalog == null || !this.catalog.equals(other.catalog))) {
      return false;
    }
    if (this.schema != other.schema && (this.schema == null || !this.schema.equals(other.schema))) {
      return false;
    }
    return true;
  }

  @Override
  public int hashCode() {
    int hash = 5;
    hash = 11 * hash + this.name.hashCode();
    hash = 11 * hash + (this.catalog != null ? this.catalog.hashCode() : 0);
    hash = 11 * hash + (this.schema != null ? this.schema.hashCode() : 0);
    return hash;
  }

  @Override
  public int compareTo(Table o) {
    final Table other = (Table) o;

    int result = (this.catalog == other.catalog) ? 0 : ((this.catalog == null) ? -1 : this.catalog.compareTo(other.catalog));

    if (result != 0) {
      result = (this.schema == other.schema) ? 0 : ((this.schema == null) ? -1 : this.schema.compareTo(other.schema));

      if (result != 0) {
        result = this.name.compareTo(other.name);
      }
    }
    return result;
  }

  public void addColumn(String colName, int dataType, String typeName, int colSize, boolean nullable) {
    final Column col = new Column(colName, dataType, typeName, colSize, nullable);

    columns.put(colName, col);

    col.setTable(this);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(catalog);
    sb.append(", ");
    sb.append(schema);
    sb.append(", ");
    sb.append(name);
    sb.append(", [");
    for (Column col : columns.values()) {
      sb.append(" [");
      if (primaryKeys.contains(col.getColName())) {
        sb.append(" (PK) ");
      }
      sb.append(col);
      sb.append("] ");
    }
    sb.append("] ");

    return sb.toString();
  }
}
